import { BaseModel, ModelAttributes, WhereCondition, OrderBy, PaginationOptions } from './BaseModel';

export interface ChatMessageAttributes {
  id?: number;
  user_id: number;
  character_id: number;
  is_user_message: boolean;
  content: string;
  created_at?: Date;
}

export class ChatMessage extends BaseModel {
  constructor() {
    super('chat_message');
  }

  
  /**
   * 获取用户与角色的聊天记录
   * @param userId 用户ID
   * @param characterId 角色ID
   * @param pagination 分页选项
   */
  async getUserCharacterChatHistory(
    userId: number,
    characterId: number,
    pagination: PaginationOptions
  ): Promise<{ data: ChatMessageAttributes[]; total: number; page: number; limit: number; totalPages: number }> {
    const where = { user_id: userId, character_id: characterId };
    const orderBy = [{ field: 'created_at', direction: 'ASC' as const }];
    
    return await this.findWithPagination(where, pagination, '*', orderBy);
  }

  
  /**
   * 获取最近的聊天记录
   * @param userId 用户ID
   * @param characterId 角色ID
   * @param limit 限制返回的记录数
   */
  async getRecentMessages(
    userId: number,
    characterId: number,
    limit: number = 10
  ): Promise<ChatMessageAttributes[]> {
    const where = { user_id: userId, character_id: characterId };
    const orderBy = [{ field: 'created_at', direction: 'DESC' as const }];
    
    return await this.findMany(where, '*', orderBy, limit);
  }

  
  /**
   * 获取用户所有聊天记录
   * @param userId 用户ID
   * @param pagination 分页选项
   */
  async getUserAllChatHistory(
    userId: number,
    pagination: PaginationOptions
  ): Promise<{ data: ChatMessageAttributes[]; total: number; page: number; limit: number; totalPages: number }> {
    const where = { user_id: userId };
    const orderBy = [{ field: 'created_at', direction: 'DESC' as const }];

    return await this.findWithPagination(where, pagination, '*', orderBy);
  }
}